﻿package com.iRomantic.common.util;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
//import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
//import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import  com.iRomantic.common.util.MyAuthenticator;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;

import sun.misc.BASE64Encoder;
public class Email extends JFinalConfig{
	/*初始化信息*/
	private  String host;//发件人使用发邮件的电子信箱服务器
	private  Properties props;
	private  MyAuthenticator auth;
	private  Session session;
	
	
	/*发件人邮箱地址*/
	private  Address fromAddress;
	
	/*邮件内容信息*/
	private  MimeMessage mimeMessage = null;
	private  MimeMultipart mmPart = null;
	
	/*附件信息*/
	private  DataSource mds = null;//为了获取附件的数据源	
	private  DataHandler dh = null;//创建引用指定 DataSource 的 DataHandler 实例
	private  BASE64Encoder enc = new BASE64Encoder();
	
		
	public Email() throws UnsupportedEncodingException, MessagingException  {
		this.props = loadPropertyFile("classes\\Email.properties");
		String mailServerName=this.props.getProperty("username");
		String password = this.props.getProperty("password");
		System.out.println(mailServerName+"--"+password);
		this.getSession(mailServerName, password, "来自爱浪漫官网邮件");
	}

	/*初始化邮箱服务器*/
	public void initServe(String mailServerName){
		this.host = "smtp."+mailServerName+".com";
		this.props = new Properties();
		this.props.put("mail.smtp.host", host);
		this.props.put("mail.smtp.auth", "true");		
	}
	
	/*登录会话*/
	public Session getSession(String mailName,String password,String head) throws UnsupportedEncodingException, MessagingException{
		String mailServerName = mailName.substring(
				mailName.lastIndexOf("@")+1,mailName.lastIndexOf("."));
		this.initServe(mailServerName);
		this.auth = new MyAuthenticator(mailName, password);
		this.session = Session.getDefaultInstance(this.props, this.auth);
		//this.session.setDebug(true);
		this.fromAddress = new InternetAddress(mailName,head);
		this.initMessage().setFrom(this.fromAddress);
		return this.session;
	}	
	
	public MimeMessage initMessage(){//单例，邮箱信息
		if(null == this.mimeMessage)
			this.mimeMessage = new MimeMessage(this.session);
		return this.mimeMessage;
	}
	
	public MimeMultipart getMMpart(){//单例，信息的组合框架
		if(null == this.mmPart)
			this.mmPart = new MimeMultipart();
		return this.mmPart;
	}
	
	/*邮件标题*/
	public void writeHead(String head) throws MessagingException{
		this.initMessage().setSubject(head);
	}
	
	/*邮件正文*/
	public void writeContent(String content) throws MessagingException{
		BodyPart bp = new MimeBodyPart();
		bp.setText(content);
		this.getMMpart().addBodyPart(bp);
	}
	
	/*附件*/
	public void writeAttchments(String path) throws MessagingException{
		File file = new File(path);
		BodyPart attachment = new MimeBodyPart();//附件
		this.mds = new FileDataSource(file);//获取附件的数据源
		this.dh = new DataHandler(this.mds);
		attachment.setDataHandler(this.dh);//锁定数据源	
		/*把附件标题进行编码，要和本工程编码一致*/
		attachment.setFileName( " =?UTF-8?B? " +
				this.enc.encode(file.getName().getBytes()) + " ?= " );
		
		this.getMMpart().addBodyPart(attachment);
	}
	
	/*发送给谁*/
	public void writeTo(String toAddress) throws MessagingException{
		Address toAdd = new InternetAddress(toAddress);
		this.initMessage().addRecipient(Message.RecipientType.TO,
				toAdd);
	}
	
	/*保存信息*/	
	public void saveMessage() throws MessagingException{
		this.initMessage().setContent(
				this.getMMpart(),
				"text/plain;charset=gbk");
		this.initMessage().setSentDate(new Date());
		this.initMessage().saveChanges();		
	}
	
	/*最后发送*/
	public void send() throws MessagingException{
		Transport.send(this.initMessage());
	}

	@Override
	public void configConstant(Constants me) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void configRoute(Routes me) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void configPlugin(Plugins me) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void configInterceptor(Interceptors me) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void configHandler(Handlers me) {
		// TODO Auto-generated method stub
		
	}
	
}
